# -*- coding:utf-8 -*-
import json
import os
from typing import Tuple, Dict, List

import munch
import yaml
from munch import Munch

from Util import config


class LoadTestData(object):

	def __init__(self):
		pass

	# ---------- 一层目录 ----------

	# 获取 data_path
	@staticmethod
	def get_jsonDataPath(path: str) -> str:
		name = path.split('TestCases')[1].replace('Test', 'Data')
		return f"{config.testDataPath}{name.replace('.py', '.json')}"

	@staticmethod
	def get_ymlDataPath(path: str) -> str:
		name = path.split('TestCases')[1].replace('Test', 'Data')
		return f"{config.testDataPath}{name.replace('.py', '.yml')}"

	# ---------- 加载通用数据目录 ----------

	@staticmethod
	def getDataPath(path, dataType):
		return f"{config.testDataPath}{path.split('TestCases')[1].replace('Test', 'Data')}.{dataType}"

	# 加载 Data 目录下json数据
	@staticmethod
	def get_jsonTestData(path, suiteName) -> Tuple[Dict, List]:
		with open(path, encoding='UTF-8') as f:
			data = json.load(f)
			caseName = [i["caseName"] for i in data[suiteName]]
		return data[suiteName], caseName

	# 加载 Data 目录下yaml数据
	@staticmethod
	def get_ymlTestData(path, suiteName=None) -> Tuple[Munch, List]:
		path = LoadTestData.get_ymlDataPath(path)
		with open(path, 'r', encoding='utf-8') as f:
			cfg = f.read()
		data = yaml.load(cfg, Loader=yaml.FullLoader)
		if suiteName:
			caseName = [i["caseName"] for i in data[suiteName]]
			return munch.munchify(data[suiteName]), caseName
		else:
			return munch.munchify(data)


loadTestData = LoadTestData()

if __name__ == '__main__':
	print(loadTestData.get_jsonDataPath("D:/lc/pythonProject/UIAutoTest/TestCases/loginPageTest"))
